package com.example.demo.hutool.db;

import cn.hutool.core.lang.func.VoidFunc1;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.sql.Condition;
import com.alibaba.fastjson.JSONObject;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class Demo1 {

    public static void main(String[] args) {


        int count = 0;
        for (int i = 0; i < 10; i++) {
            count++;
        }

        System.out.println(count);

        int[] arr = {1, 5, 9, 2, 6, 10, 3, 7, 4};

        for (int i = 1; i < arr.length; i++) { //遍历n次
            for (int j = 0; j < arr.length - i; j++) {//遍历n-1次
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        System.out.println(JSONObject.toJSONString(arr));

        int[] arr1 = {1, 2, 3, 4, 6, 10, 13, 15, 18, 30, 33, 39, 43, 50, 56};

        System.out.println(binarySearch(arr1, 10));




        /*try {

            List<Entity> list = Db.use().findAll("order");
            System.out.println(list);
            int i = 7000;
            while (i < 6000) {
                i++;

                int id = i;
                String code = String.valueOf(i);
                String name = RandomUtil.randomEle(Arrays.asList("蓝牙耳机", "有线耳机"));
                String city = RandomUtil.randomEle(Arrays.asList("北京", "上海", "广州", "杭州", "重庆"));
                int num = RandomUtil.randomInt(1, 100);

                Db.use().execute("insert into `order` values (?, ?, ?, ?, ?, ?)", id, code, name, "2021-06-20 16:46:00", city, num);
            }
            // 查询全部
            //List<Entity> list = Db.use().findAll("Company");
            //System.out.println(list);
            // 条件查询
            //List<Entity> list = Db.use().findAll(Entity.create("Company").set("CompanyID", 5500));
            //System.out.println(list);
            // 模糊查询
            //List<Entity> list = Db.use().findLike("Company", "CompanyName", "上海", Condition.LikeType.StartWith);
            //System.out.println(list);
            // 分页查询
            //PageResult<Entity> pageResult = Db.use().page(Entity.create("Company").set("CompanyID", "> 5500"), new Page(2, 20));
            //System.out.println(pageResult);
            // 执行curd语句
            //List<Entity> result = Db.use().query("select * from user where age < ?", 3);
            //Db.use().execute("insert into user values (?, ?, ?)", "张三", 17, 1);
            // 事务
            //Db.use().tx(new VoidFunc1<Db>() {
            //    @Override
            //    public void call(Db db) throws Exception {
            //        db.insert(Entity.create("Company").set("CompanyName", 111));
            //        db.update(Entity.create("Company").set("CompanyName", "66666666"), Entity.create("Company").set("CompanyID", 5500));
            //    }
            //});
            // in查询
            // Db.use().findAll(Entity.create("Company").set("CompanyID", new long[]{1, 2, 3}));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }*/

        int[] arr2 = {50, 18, 30, 33, 1, 13, 15, 39, 2, 3, 43, 56, 4, 6, 10};
        System.out.println(JSONObject.toJSONString(mergeSort(arr2, 0, 14)));
    }

    // 归并排序 (待排序数组,从哪个脚标开始，到哪个脚标结束)
    public static int[] mergeSort(int[] arr, int low, int high) {
        int mid = (low + high) / 2;
        if (low < high) {
            mergeSort(arr, low, mid);
            mergeSort(arr, mid + 1, high);
            merge(arr, low, mid, high);
        }
        return arr;
    }

    private static void merge(int[] arr, int low, int mid, int high) {
        int[] temp = new int[high - low + 1];
        int i = low, j = mid + 1, k = 0;
        while (i <= mid && j <= high) {
            if (arr[i] <= arr[j]) {
                temp[k++] = arr[i++];
            } else {
                temp[k++] = arr[j++];
            }
        }
        while (i <= mid) {
            temp[k++] = arr[i++];
        }
        while (j <= high) {
            temp[k++] = arr[j++];
        }
        for (int x = 0; x < temp.length; x++) {
            arr[x + low] = temp[x];
        }
    }

    // 二分查找
    static int binarySearch(int[] arr, int key) {
        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] > key) {
                high = mid - 1;
            } else if (arr[mid] < key) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
